/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * Nihongo
 * Copyright (C) Massimiliano Maniscalco 2011, 2012 <massi.neptune@yahoo.com>
 * 
 * Nihongo is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Nihongo is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _TESTCONTROLLER_H_
#define _TESTCONTROLLER_H_

#include <gtkmm.h>
#include "test_parole_model.h"
#include "test_kanji_model.h"
#include "popup_button_model.h"

class Parola;
class Kanji;
class Lezione;
class TestEngine;

using namespace std;

class TestController
{
public:
	inline TestController () :
		_parole ( 0 ), _kanji ( 0 ), _testSelWin ( 0 )
	{}
	inline TestController ( Glib::RefPtr<Gtk::Builder> _bldr );
	inline ~TestController ();
	void present_window ( void );
	void init_window ( void );
	inline void parole ( list<Parola> *pp ) { _parole = pp; }
	inline void kanji ( list<Kanji> *pk ) { _kanji = pk; }
	inline void lezioni ( list<Lezione> *pl ) { _lezioni = pl; }
	// vox signal
	typedef sigc::signal<void> Type_signal_vox;
	inline Type_signal_vox signal_items_changed ( void ) { return commit_vox_changed; }

protected:
	void update_messaggio_statusbar ( int _ni, const char *_elemento, const char *_elementi );
	void crea_stringa_tempo_stimato ( int _ni, Glib::ustring &_str );
	void connect_signals ( void );
	void on_main_page_switch ( Gtk::Widget *page, guint page_num );
	void on_window_closed ( void );
	void load_pop_up_button ( vector<Glib::ustring>::iterator _start, vector<Glib::ustring>::iterator _end, Glib::RefPtr<Gtk::ListStore> _popUpList );
	void load_lezione_pop_up_button ( Glib::RefPtr<Gtk::ListStore> &_popUpList );
	void update_lezione_pop_up_button ( Gtk::ComboBox *_popUpButton, sigc::connection &_buttonBind, Glib::RefPtr<Gtk::ListStore> _listPopUpStore, void (TestController::*loadFunc)() );
	list<Lezione>::iterator get_lezione_from_pop_up_button ( Gtk::ComboBox *_popUpButton );
	// toolbar
	void on_start_clicked ( void );
	// page parole
	void load_parole_in_tree_view ( void );
	void on_parole_modo_tipo_radio_button_toggled ( Gtk::RadioButton *_button );
	void on_coniugazioni_tipo_radio_button_toggled ( Gtk::RadioButton *_button );
	void on_cortese_radio_button_toggled ( Gtk::RadioButton *_button );
	void on_negativo_radio_button_toggled ( Gtk::RadioButton *_button );
	void on_parole_mode_changed ( void );
	void on_parole_lezione_radio_button_toggled ( Gtk::RadioButton *_button );
	void on_parole_tipo_changed ( void );
	void on_parole_date_changed ( void );
	void on_parole_enable_valutazione_toggled ( void );
	void on_parole_value_changed ( void );
	void prepara_selezione_parole ( list<Parola> *_selezionate );
	void on_signal_parole_changed ( const Parola &verbum );
	// page kanji
	void load_kanji_in_tree_view ( void );
	void on_kanji_modo_radio_button_toggled ( Gtk::RadioButton *_button );
	void on_kanji_lezione_radio_button_toggled ( Gtk::RadioButton *_button );
	void on_kanji_date_changed ( void );
	void on_kanji_enable_valutazione_toggled ( void );
	void on_kanji_value_changed ( void );
	void prepara_selezione_kanji ( list<Kanji> *_selezionate );
	void on_signal_kanji_changed ( const Kanji &knj );
	void on_kanji_kaku_clicked ( Gtk::Button *_button );
	void on_printable_window_closed ( Gtk::Window *_win );
	// page numeri
	void on_numeri_modo_radio_button_toggled ( Gtk::RadioButton *_button );
	void on_numeri_numero_domande_changed ( void );

private:
	Glib::RefPtr<Gtk::Builder> _builder;
	list<Parola> *_parole;
	list<Kanji> *_kanji;
	list<Lezione> *_lezioni;
	Type_signal_vox commit_vox_changed;
	Gtk::Window *_testSelWin;
	Gtk::Notebook *_testNotebook;
	unsigned short _testPage;
	PopUpButtonModel* _buttonPopUpColumn;
	Glib::RefPtr<Gtk::ListStore> _testDatePopUpStore;
	TestEngine *_testCtrl;
	sigc::connection _testCtrlVoxChangedConnection;
	sigc::connection _testCtrlWinClosedConnection;
	Gtk::Statusbar *_mainStatus;
	int _stsTestSelId;
	// toolbar
	Gtk::ToolButton *_startTestButton;
	// page parole
	TestParoleModel* _paroleListColumns;
	Glib::RefPtr<Gtk::ListStore> _paroleListStore;
	Gtk::TreeView* _paroleTestList;
	Gtk::RadioButton *_testParModeTipoRadioBtn1;
	Gtk::RadioButton *_testParModeTipoRadioBtn2;
	char _testParoleModoTipo;
	Gtk::HButtonBox *_testConiuTipoBox;
	Gtk::RadioButton *_testConiuTipoRadioBtn1;
	Gtk::RadioButton *_testConiuTipoRadioBtn2;
	Gtk::HButtonBox *_testCortBox;
	Gtk::RadioButton *_testCortRadioBtn1;
	Gtk::RadioButton *_testCortRadioBtn2;
	Gtk::RadioButton *_testCortRadioBtn3;
	char _testParoleCortese;
	Gtk::HButtonBox *_testNegBox;
	Gtk::RadioButton *_testNegRadioBtn1;
	Gtk::RadioButton *_testNegRadioBtn2;
	Gtk::RadioButton *_testNegRadioBtn3;
	char _testParoleAffermativa;
	Gtk::ComboBox *_testParModeButton;
	Glib::RefPtr<Gtk::ListStore> _testParModePopUpStore;
	char _testParoleModalita;
	vector<Glib::ustring> *_testParModeStrings;
	char _testParoleLezione;
	Gtk::ComboBox *_testParVersoButton;
	Glib::RefPtr<Gtk::ListStore> _testParVersoPopUpStore;
	vector<Glib::ustring> *_testParVersoStrings;
	Gtk::ComboBox *_testParLezioneButton;
	sigc::connection _testParLezioneConnection;
	Glib::RefPtr<Gtk::ListStore> _testParLezionePopUpStore;
	Gtk::RadioButton *_testParLezioneRadioBtn1;
	Gtk::RadioButton *_testParLezioneRadioBtn2;
	Gtk::RadioButton *_testParLezioneRadioBtn3;
	vector<Glib::ustring> *_testParTipoStrings;
	Gtk::ComboBox *_testParTipoButton;
	Glib::RefPtr<Gtk::ListStore> _testParTipoPopUpStore;
	char _testParoleTipo;
	Gtk::ComboBox *_testParDateButton;
	char _testParoleData;
	Gtk::CheckButton *_testParEnableValButton;
	bool _testParoleEnableVal;
	Gtk::HScale *_testParValScale;
	short _testParoleValutazione;
	// page kanji
	TestKanjiModel* _kanjiListColumns;
	Glib::RefPtr<Gtk::ListStore> _kanjiListStore;
	Gtk::TreeView* _kanjiTestList;
	Gtk::RadioButton *_testKnjModeRadioBtn1;
	Gtk::RadioButton *_testKnjModeRadioBtn2;
	Gtk::RadioButton *_testKnjModeRadioBtn3;
	char _testKanjiModo;
	char _testKanjiLezione;
	Gtk::ComboBox *_testKnjVersoButton;
	Glib::RefPtr<Gtk::ListStore> _testKnjVersoPopUpStore;
	vector<Glib::ustring> *_testKnjVersoStrings;
	Gtk::ComboBox *_testKnjLezioneButton;
	sigc::connection _testKnjLezioneConnection;
	Glib::RefPtr<Gtk::ListStore> _testKnjLezionePopUpStore;
	Gtk::RadioButton *_testKnjLezioneRadioBtn1;
	Gtk::RadioButton *_testKnjLezioneRadioBtn2;
	Gtk::RadioButton *_testKnjLezioneRadioBtn3;
	Gtk::ComboBox *_testKnjDateButton;
	unsigned char _testKanjiData;
	Gtk::CheckButton *_testKnjEnableValButton;
	bool _testKanjiEnableVal;
	Gtk::HScale *_testKnjValScale;
	short _testKanjiValutazione;
	Gtk::Button *_kakiKataButton;
	Gtk::Button *_oboeteIruButton;
	// page numeri
	Gtk::RadioButton *_testNmrModoRadioBtn1;
	Gtk::RadioButton *_testNmrModoRadioBtn2;
	Gtk::RadioButton *_testNmrModoRadioBtn3;
	char _testNmrModo;
	Gtk::ComboBox *_testNmrVersoButton;
	Glib::RefPtr<Gtk::ListStore> _testNmrVersoPopUpStore;
	Gtk::Entry *_testNmrEntry;
	Gtk::ComboBox *_testNmrMaxValButton;
	Glib::RefPtr<Gtk::ListStore> _testNmrMaxValPopUpStore;

};

inline TestController::
TestController ( Glib::RefPtr<Gtk::Builder> _bldr ) :
	_builder ( _bldr ), _testCtrl ( 0 )
{
	_testPage = 0;
	_bldr->get_widget("testSelectionWin", _testSelWin);
	_bldr->get_widget ("testNotebook", _testNotebook);
	_buttonPopUpColumn = new PopUpButtonModel ();
	_testDatePopUpStore = Gtk::ListStore::create(*_buttonPopUpColumn);
	_bldr->get_widget ("testStatusbar", _mainStatus);
	_stsTestSelId = _mainStatus->get_context_id ( Glib::ustring ( "TEST_SEL" ) );
	// toolbar
	_bldr->get_widget ("startTestButton", _startTestButton);
	// page parole
	_paroleListColumns = new TestParoleModel ();
	_paroleListStore = Gtk::ListStore::create(*_paroleListColumns);
	_bldr->get_widget ("testParoleListView", _paroleTestList);
	_paroleTestList->set_model(_paroleListStore);
	_bldr->get_widget ("testParModeTipoRadiobutton1", _testParModeTipoRadioBtn1);
	_bldr->get_widget ("testParModeTipoRadiobutton2", _testParModeTipoRadioBtn2);
	_testParModeTipoRadioBtn1->set_active ( true );
	_testParoleModoTipo = 0;
	_bldr->get_widget ("testConiuHbuttonbox", _testConiuTipoBox);
	_bldr->get_widget ("testConiuTipoRadiobutton1", _testConiuTipoRadioBtn1);
	_bldr->get_widget ("testConiuTipoRadiobutton2", _testConiuTipoRadioBtn2);
	_testConiuTipoRadioBtn1->set_active ( true );
	_bldr->get_widget ("testCortHbuttonbox", _testCortBox);
	_bldr->get_widget ("testCortRadiobutton1", _testCortRadioBtn1);
	_bldr->get_widget ("testCortRadiobutton2", _testCortRadioBtn2);
	_bldr->get_widget ("testCortRadiobutton3", _testCortRadioBtn3);
	_testParoleCortese = 1;
	_testCortRadioBtn1->set_active ( true );
	_bldr->get_widget ("testNegHbuttonbox", _testNegBox);
	_bldr->get_widget ("testNegRadiobutton1", _testNegRadioBtn1);
	_bldr->get_widget ("testNegRadiobutton2", _testNegRadioBtn2);
	_bldr->get_widget ("testNegRadiobutton3", _testNegRadioBtn3);
	_testParoleAffermativa = 1;
	_testNegRadioBtn1->set_active ( true );
	_bldr->get_widget ("testParModoCombobox", _testParModeButton);
	_testParModePopUpStore = Gtk::ListStore::create(*_buttonPopUpColumn);
	_testParModeButton->set_model (_testParModePopUpStore);
	Glib::ustring _parModo[24] = { "Memorizzazione", "Sintassi", "Lettura", "Tutte le coniugazioni", "Presente", "Passato", "Sospensivo", "Causale", "Avverbiale", "Diventare", "Rendere", "Tutte le coniugazioni", "Presente", "Passato", "Forma in て", "Desiderativo", "Volitivo", "Esortativo", "Imperativo", "Obbligo", "Potenziale", "Passivo", "Causativo", "Condizionale" };
	_testParModeStrings = new vector<Glib::ustring> ( _parModo, _parModo + 24 );
	load_pop_up_button ( _testParModeStrings->begin(), _testParModeStrings->end() - 21, _testParModePopUpStore );
	_testParModeButton->set_active ( 0 );
	_testParoleModalita = 0;
	_bldr->get_widget ("testParoleCombobox1", _testParVersoButton);
	_testParVersoPopUpStore = Gtk::ListStore::create(*_buttonPopUpColumn);
	_testParVersoButton->set_model (_testParVersoPopUpStore);
	Glib::ustring _parVerso[4] = { "giapponese -> italiano", "italiano -> giapponese", "kanji -> hiragana", "forma base -> forma coniugata" };
	_testParVersoStrings = new vector<Glib::ustring> ( _parVerso, _parVerso + 4 );
	load_pop_up_button ( _testParVersoStrings->begin(), _testParVersoStrings->end() - 2, _testParVersoPopUpStore );
	_testParVersoButton->set_active ( 0 );
	_bldr->get_widget ("testParoleCombobox2", _testParLezioneButton);
	_testParLezionePopUpStore = Gtk::ListStore::create(*_buttonPopUpColumn);
	_testParLezioneButton->set_model (_testParLezionePopUpStore);
	_bldr->get_widget ("testParoleRadiobutton4", _testParLezioneRadioBtn1);
	_bldr->get_widget ("testParoleRadiobutton5", _testParLezioneRadioBtn2);
	_bldr->get_widget ("testParoleRadiobutton6", _testParLezioneRadioBtn3);
	_testParLezioneRadioBtn1->set_active ( true );
	_testParoleLezione = 0;
	_bldr->get_widget ("testParoleCombobox3", _testParTipoButton);
	_testParTipoPopUpStore = Gtk::ListStore::create(*_buttonPopUpColumn);
	_testParTipoButton->set_model (_testParTipoPopUpStore);
	Glib::ustring _parTipo[20] = { "qualunque tipo", "nome", "agg.vo i", "agg.vo na", "v. godan tr.", "v. godan intr.", "v. ichidan tr.", "v. ichidan intr.", "v. irregolare", "posposizione", "avverbio", "qualunque aggettivo", "agg.vo i", "agg.vo na", "qualunque verbo", "v. godan tr.", "v. godan intr.", "v. ichidan tr.", "v. ichidan intr.", "v. irregolare" };
	_testParTipoStrings = new vector<Glib::ustring> ( _parTipo, _parTipo + 20 );
	load_pop_up_button ( _testParTipoStrings->begin(), _testParTipoStrings->end() - 9, _testParTipoPopUpStore );
	_testParTipoButton->set_active ( 0 );
	_testParoleTipo = 0;
	_bldr->get_widget ("testParoleCombobox4", _testParDateButton);
	_testParDateButton->set_model (_testDatePopUpStore);
	_testParoleData = 0;
	_bldr->get_widget ("testParoleCheck1", _testParEnableValButton);
	_testParoleEnableVal = false;
	_bldr->get_widget ("testParoleScale1", _testParValScale);
	_testParoleValutazione = 0;
	// page kanji
	_kanjiListColumns = new TestKanjiModel ();
	_kanjiListStore = Gtk::ListStore::create(*_kanjiListColumns);
	_bldr->get_widget ("testKanjiListView", _kanjiTestList);
	_kanjiTestList->set_model(_kanjiListStore);
	_bldr->get_widget ("testKanjiRadiobutton1", _testKnjModeRadioBtn1);
	_bldr->get_widget ("testKanjiRadiobutton2", _testKnjModeRadioBtn2);
	_bldr->get_widget ("testKanjiRadiobutton3", _testKnjModeRadioBtn3);
	_testKnjModeRadioBtn1->set_active ( true );
	_testKanjiModo = 0;
	_bldr->get_widget ("testKanjiCombobox1", _testKnjVersoButton);
	_testKnjVersoPopUpStore = Gtk::ListStore::create(*_buttonPopUpColumn);
	_testKnjVersoButton->set_model (_testKnjVersoPopUpStore);
	Glib::ustring _knjVerso[3] = { "kanji -> significato", "significato -> kanji", "kanji -> lettura" };
	_testKnjVersoStrings = new vector<Glib::ustring> ( _knjVerso, _knjVerso + 3 );
	load_pop_up_button ( _testKnjVersoStrings->begin(), _testKnjVersoStrings->end() - 1, _testKnjVersoPopUpStore );
	_testKnjVersoButton->set_active ( 0 );
	_bldr->get_widget ("testKanjiCombobox2", _testKnjLezioneButton);
	_testKnjLezionePopUpStore = Gtk::ListStore::create(*_buttonPopUpColumn);
	_testKnjLezioneButton->set_model (_testKnjLezionePopUpStore);
	_bldr->get_widget ("testKanjiRadiobutton3", _testKnjLezioneRadioBtn1);
	_bldr->get_widget ("testKanjiRadiobutton4", _testKnjLezioneRadioBtn2);
	_bldr->get_widget ("testKanjiRadiobutton5", _testKnjLezioneRadioBtn3);
	_testKnjLezioneRadioBtn1->set_active ( true );
	_testKanjiLezione = 0;
	_bldr->get_widget ("testKanjiCombobox3", _testKnjDateButton);
	_testKnjDateButton->set_model (_testDatePopUpStore);
	_testKanjiData = 0;
	_bldr->get_widget ("testKanjiCheck1", _testKnjEnableValButton);
	_testKanjiEnableVal = false;
	_bldr->get_widget ("testKanjiScale1", _testKnjValScale);
	_testKanjiValutazione = 0;
	Glib::ustring _dateOpt[4] = { "non considerare la data", "più di una settimana", "più di un mese", "più di un anno" };
	vector<Glib::ustring> _dateOpts ( _dateOpt, _dateOpt + 4 );
	load_pop_up_button ( _dateOpts.begin(), _dateOpts.end(), _testDatePopUpStore );
	_testParDateButton->set_active ( 0 );
	_testKnjDateButton->set_active ( 0 );
	_bldr->get_widget ("testKanjiButton1", _kakiKataButton);
	_bldr->get_widget ("testKanjiButton2", _oboeteIruButton);
	// page numeri
	_bldr->get_widget ("testNumeriRadiobutton1", _testNmrModoRadioBtn1);
	_bldr->get_widget ("testNumeriRadiobutton2", _testNmrModoRadioBtn2);
	_bldr->get_widget ("testNumeriRadiobutton3", _testNmrModoRadioBtn3);
	_testNmrModoRadioBtn1->set_active ( true );
	_testNmrModo = 0;
	_bldr->get_widget ("testNumeriCombobox1", _testNmrVersoButton);
	_testNmrVersoPopUpStore = Gtk::ListStore::create(*_buttonPopUpColumn);
	_testNmrVersoButton->set_model (_testNmrVersoPopUpStore);
	Glib::ustring _numVerso[3] = { "da cifre a hiragana", "da hiragana a cifre", "da kanji a hiragana" };
	vector<Glib::ustring> _numVersoS ( _numVerso, _numVerso + 3 );
	load_pop_up_button ( _numVersoS.begin(), _numVersoS.end() - 1, _testNmrVersoPopUpStore );
	_testNmrVersoButton->set_active ( 0 );
	_bldr->get_widget ("testNumeriEntry1", _testNmrEntry);
	_testNmrEntry->set_text ( "10" );
	_bldr->get_widget ("testNumeriCombobox2", _testNmrMaxValButton);
	_testNmrMaxValPopUpStore = Gtk::ListStore::create(*_buttonPopUpColumn);
	_testNmrMaxValButton->set_model (_testNmrMaxValPopUpStore);
	Glib::ustring _maxVal[5] = { "10", "99", "999", "9.999", "99.999.999" };
	vector<Glib::ustring> _maxVals ( _maxVal, _maxVal + 5 );
	load_pop_up_button ( _maxVals.begin(), _maxVals.end(), _testNmrMaxValPopUpStore );
	_testNmrMaxValButton->set_active ( 0 );
	
	connect_signals ();
}

inline TestController::
~TestController ()
{
	delete _paroleListColumns;
	delete _kanjiListColumns;
	delete _buttonPopUpColumn;
	delete _testParModeStrings;
	delete _testParVersoStrings;
	delete _testParTipoStrings;
	delete _testKnjVersoStrings;
}

#endif // _TESTCONTROLLER_H_
